package com.second.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

    /**
     * Date->指定pattern的String
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {//Date->String
        if (date == null)//考虑date这个数据可能获取失败
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 将字符串解析成日期
     *
     * @param strDate
     * @param pattern
     * @return
     */
    public static Date parse(String strDate, String pattern) {
        if (strDate == null || strDate.trim().length() == 0)
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
